package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.flqy.baselibrary.utils.L;
import com.google.android.material.tabs.TabLayout;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.CityPartnerAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.CityMasterBaseBean;
import com.shenzhen.horselive.beans.CodeBean;
import com.shenzhen.horselive.beans.ProfitRankingBean;
import com.shenzhen.horselive.beans.TeQuanBean;
import com.shenzhen.horselive.databinding.ActivityCityPartnerBinding;
import com.shenzhen.horselive.databinding.HeadCityPartnerBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.utils.GetJsonDataUtil;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;

import org.xml.sax.helpers.XMLReaderAdapter;

import java.util.ArrayList;
import java.util.List;

public class CityPartnerActivity extends AppBarActivity {

    ActivityCityPartnerBinding  binding ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCityPartnerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        initViews();
        //获取收益排行榜和城市价格排行榜
        initdata();
    }

    TencentLocationManager  locationManager;
    private void initdata() {
        //收益排行
        getProfitRanking();
        //获取城主权益
        ApiFactory.getApiService().tequan(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<TeQuanBean>(this) {
                    @Override
                    protected void onNextInActive(TeQuanBean cityMasterBaseBean) {
                        //百分比相关的字段介绍
                        if (cityMasterBaseBean.isSuccess())
                        {


//                            获得城市内所有会员领红包总额的5%
//                                    获得城市内所有话费充值金额的10%
//                                    获得城市内开卡奖励总额的5%
//                                    获得城市内矿主收益的5%
//                                    获得城市内兑换矿证/兑换金卡总额的5%
//                                    获得城市内支付公排/收钱吧收益总额的5%
//                                    市级城主收益中包涵区，县城主收益的60%

                            headbinding.tvFeeText.setText("获得城市内所有会员领红包总额的"+cityMasterBaseBean.getData().getGetRatio()+"%");
                            headbinding.tvFeeText1.setText("获得城市内所有话费充值金额的"+cityMasterBaseBean.getData().getUserPhoneBillRatio()+"%");
                            headbinding.tvFeeText2.setText("获得城市内开卡奖励总额的"+cityMasterBaseBean.getData().getIncreasedFzRatio()+"%");
                            headbinding.tvFeeText3.setText("获得城市内矿主收益的"+cityMasterBaseBean.getData().getMinersProfitRatio()+"%");
                            headbinding.tvFeeText4.setText("获得城市内兑换矿证/兑换金卡总额的"+cityMasterBaseBean.getData().getUserConvertGoldCardRatio()+"%");
                            headbinding.tvFeeText5.setText("获得城市内支付公排/收钱吧收益总额的"+cityMasterBaseBean.getData().getUserPosRatio()+"%");
                            headbinding.tvFeeText6.setText("市级城主收益中包涵区，县城主收益的"+cityMasterBaseBean.getData().getAreaInCityRatio()+"%");
//                            headbinding.tvFeeText6.setText("备注：若市级城主范围产生了区县级城主，分配比例为：市级"+cityMasterBaseBean.getData().getCityGetRatio()+"%，区县"+cityMasterBaseBean.getData().getAreaGetRatio()+"%");
                        }
                    }
                });

    }



    CityPartnerAdapter  adapter;
    int  type;
    private void initViews() {
         type = getIntent().getIntExtra("type",0);
        if (type==1)
        {
            setTitle("我的主城");
            binding.btGo.setVisibility(View.GONE);
        }else
        {
            setTitle("城市合伙人");
            setMenuText("申请历史");
        }
        locationManager  = TencentLocationManager.getInstance(this);
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter =new CityPartnerAdapter(R.layout.item_citypater);
        binding.recyclerview.setAdapter(adapter);
        adapter.addHeaderView(initHead());
        binding.btGo.setOnClickListener(onClickListener);
    }

    long  lasttime;
    private View.OnClickListener  onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            long nowtime =  System.currentTimeMillis();
            if ((nowtime-lasttime)/1000<1)
            {
                return;
            }
            lasttime=  nowtime;
            switch (v.getId())
            {
                case R.id.bt_go:
                    startActivity(new Intent(CityPartnerActivity.this,ApplyCityActivity.class));
                    break;
            }

        }
    };


    //初始化头部
    HeadCityPartnerBinding  headbinding;
    private View initHead() {
         headbinding = HeadCityPartnerBinding.inflate(getLayoutInflater());
        headbinding.tabs.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition())
                {
                    case 0:
                        getProfitRanking();
                        break;
                    case 1:
                        getPriceRankingList();
                        break;
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        if (type==1)
        {
           headbinding.container.setVisibility(View.VISIBLE);
           headbinding.imgHeadImg.setVisibility(View.GONE);
           headbinding.btGoHall.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                  startActivity(new Intent(getContext(),AuctionHallActivity.class));
               }
           });
        }
        return headbinding.getRoot();
    }
    //城市收益排行榜
    private void getProfitRanking() {

        ApiFactory.getApiService().profitRankingList(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<ProfitRankingBean>(this) {
                    @Override
                    protected void onNextInActive(ProfitRankingBean profitRankingBean) {
                        if (profitRankingBean.isSuccess())
                        {
                            adapter.setNewData(profitRankingBean.getData());

                        }else
                        {
                            showToast(profitRankingBean.getErrMessage());
                        }
                    }
                });
    }

    //城市价格排行榜
    private  void   getPriceRankingList()
    {
        ApiFactory.getApiService().priceRankingList(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<ProfitRankingBean>(this) {
                    @Override
                    protected void onNextInActive(ProfitRankingBean profitRankingBean) {
                        if (profitRankingBean.isSuccess())
                        {
                            adapter.setNewData(profitRankingBean.getData());

                        }else
                        {
                            showToast(profitRankingBean.getErrMessage());
                        }
                    }
                });
    }

    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();
        startActivity(new Intent(this,ApplyCityHistoryActivity.class));
    }



    //城市合伙人0，我的主城没有记录1
    public static void start(Context context,int  type) {
        Intent starter = new Intent(context, CityPartnerActivity.class);
        starter.putExtra("type",type);
        context.startActivity(starter);
    }



}